package com.flyinzhen.customer.controller;

import com.flyinzhen.core.annotation.AvoidRepeat;
import com.flyinzhen.core.annotation.IgnoreToken;
import com.flyinzhen.core.constants.ResponseStatus;
import com.flyinzhen.core.dto.ResponseDto;
import com.flyinzhen.customer.dto.input.AuthLoginInput;
import com.flyinzhen.customer.dto.input.AuthUpdateInput;
import com.flyinzhen.customer.service.AuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * 登陆认证控制器
 *
 * @author yinzhen
 * @date 2019-05-01
 */
@Api
@CrossOrigin("*")
@RestController
@RequestMapping(value = "/auth", produces = "application/json;charset=UTF-8")
public class AuthorityController {

    @Resource
    AuthorityService authorityService;

    @IgnoreToken
    @PostMapping("/login")
    @AvoidRepeat(timeout = 3)
    @ApiOperation(value = "登录", notes = "用户登录")
    public ResponseDto login(@RequestBody @Validated AuthLoginInput inputDto, BindingResult validation) {
        return validation.hasErrors() ? new ResponseDto(ResponseStatus.PARAM_INVALID) : authorityService.login(inputDto);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新", notes = "更新客户信息")
    public ResponseDto update(@RequestBody @Validated AuthUpdateInput inputDto, BindingResult validation) {
        return validation.hasErrors() ? new ResponseDto(ResponseStatus.PARAM_INVALID) : authorityService.update(inputDto);
    }

    @GetMapping("/test")
    public ResponseDto test() {
        return ResponseDto.ok("test");
    }
}